<template>
	<view class="login-container">
		<view class="login-box">
			<text class="title">用户登录</text>

			<view class="input-group">
				<input type="text" v-model="user.Account" placeholder="请输入用户名" class="input-item" />
				<input type="password" v-model="user.Password" placeholder="请输入密码" class="input-item" />
			</view>

			<button class="login-btn" @click="handleLogin">
				登录
			</button>
		</view>
	</view>
</template>

<script>
	import loginapi from '../../api/account/index.js'

	export default {
		data() {
			return {
				user: {
					Account: 'admin', // 清空默认值
					Password: '000000', // 清空默认值
					Tenant: undefined,
					RemoteToken: undefined,
				}
			}
		},
		methods: {
			// 修正方法定义
			handleLogin: async function() {
				// 简单验证
				if (!this.user.Account || !this.user.Password) {
					uni.showToast({
						title: '请输入用户名和密码',
						icon: 'none'
					})
					return
				}

				// 显示加载中
				uni.showLoading({
					title: '登录中...'
				})

				try {
					// 调用登录接口
					const result = await loginapi().signIn(this.user);
					
					// 隐藏加载
					uni.hideLoading()
					
					// 存储token
					uni.setStorageSync('token', result.access_token);
					
					// 显示成功提示
					uni.showToast({
						title: '登录成功',
						icon: 'none',
						duration: 3000
					});

					// 跳转到首页（修正路由跳转）
					this.$router.push('/pages/index/index')
					
				} catch (error) {
					// 隐藏加载
					uni.hideLoading()
					
					// 显示错误提示
					uni.showToast({
						title: '登录失败',
						icon: 'none',
						duration: 3000
					});
				}
			}
		}
	}
</script>

<style scoped>
	.login-container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f5f5f5;
	}

	.login-box {
		width: 90%;
		max-width: 600rpx; /* 调整宽度更合理 */
		padding: 60rpx;
		background-color: #fff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
	}

	.title {
		display: block;
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		text-align: center;
		margin-bottom: 60rpx;
	}

	.input-group {
		margin-bottom: 40rpx;
	}

	.input-item {
		width: 100%;
		height: 80rpx;
		padding: 0 20rpx;
		margin-bottom: 20rpx;
		border: 2rpx solid #eee;
		border-radius: 8rpx;
		font-size: 28rpx;
	}

	.login-btn {
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #007aff;
		color: #fff;
		font-size: 32rpx;
		border-radius: 8rpx;
	}
</style>
